<!-- src/components/spots/SpotItem.vue -->
<template>
  <div class="spot-item" @click="$emit('click', spot)">

    <el-button 
    class="add-button" 
    type="primary" 
    :icon="Plus" 
    circle 
    size="small"
    @click.stop="$emit('add', spot)"
  />

    <el-image :src="spot.image" class="spot-image" fit="cover">
      <template #error>
        <div class="image-placeholder">
          <el-icon>
            <Picture />
          </el-icon>
        </div>
      </template>
    </el-image>

    <div class="spot-info">
      <h3 class="spot-name">{{ spot.name }}</h3>
      <p class="spot-description">{{ spot.description }}</p>
      <div class="spot-meta">
        <span class="rating">
          <el-rate v-model="spot.attractionRating" disabled :max="5" :colors="['#ff9900', '#ff9900', '#ff9900']"
            :void-color="'#C6D1DE'" />
        </span>
        <span class="distance">
          {{ formatDistance(spot.distance) }}
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { Picture ,Plus } from '@element-plus/icons-vue'

const formatDistance = (distance) => {
  if (distance >= 1000) {
    return `${(distance / 1000).toFixed(1)}km`
  }
  return `${Math.round(distance)}m`
}

defineProps({
  spot: {
    type: Object,
    required: true,
    validator: (spot) => {
      return spot.id && spot.name && spot.image && spot.recommendedVisitTime
    }
  }
})

defineEmits(['click','add'])

const handleItemClick = (event) => {
  emit('click', spot)
}
</script>

<style scoped>
.spot-item {
  position: relative;
  display: flex;
  padding: 16px;
  margin-bottom: 12px;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #ebeef5;
}

.spot-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.spot-image {
  width: 100px;
  height: 100px;
  border-radius: 8px;
  overflow: hidden;
}

.image-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  color: #909399;
  font-size: 20px;
}

.spot-info {
  flex: 1;
  margin-left: 16px;
  display: flex;
  flex-direction: column;
}

.spot-name {
  margin: 0 0 8px;
  font-size: 16px;
  font-weight: bold;
  color: #303133;
}

.spot-description {
  margin: 0 0 12px;
  font-size: 14px;
  color: #606266;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  flex: 1;
}

.spot-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.distance {
  font-size: 14px;
  color: #909399;
}

.rating {
  display: flex;
  align-items: center;
}

:deep(.el-rate) {
  display: inline-block;
}

:deep(.el-rate__icon) {
  margin-right: 2px;
  font-size: 14px;
}


.add-button {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 1;
  transform: scale(0.8);  /* 使按钮更小 */
}

.add-button :deep(.el-icon) {
  font-size: 12px;  /* 调整图标大小 */
}
</style>